import 'package:flutter/material.dart';
import '../services/deepseek_service.dart';

class RiskAssessmentResultPage extends StatefulWidget {
  final String gender;
  final String age;
  final String currentHistory;
  final String diet;
  final String sleep;
  final String exercise;

  const RiskAssessmentResultPage({
    Key? key,
    required this.gender,
    required this.age,
    required this.currentHistory,
    required this.diet,
    required this.sleep,
    required this.exercise,
  }) : super(key: key);

  @override
  State<RiskAssessmentResultPage> createState() =>
      _RiskAssessmentResultPageState();
}

class _RiskAssessmentResultPageState extends State<RiskAssessmentResultPage> {
  final List<String> _assessmentResults = [];
  bool _isGenerating = true;
  final DeepSeekService _deepSeekService =
      DeepSeekService('sk-4e0e104af45b432d86f0b95db6681b9f');

  @override
  void initState() {
    super.initState();
    _generateAssessment();
  }

  Future<void> _generateAssessment() async {
    try {
      final prompt = '''
请根据以下用户日志信息进行健康风险评估：

性别：${widget.gender}
年龄：${widget.age}
现病史：${widget.currentHistory}
饮食情况：${widget.diet}
睡眠情况：${widget.sleep}
运动情况：${widget.exercise}

请提供以下方面的分析：
1. 健康风险评分（0%-100%，数字越高风险越高）
2. 健康建议（包括饮食、运动、作息等方面）
3. 推荐进行的医学检查

请以专业健康顾问的身份进行分析，并给出详细建议，避免使用markdown语法。''';

      final response = await _deepSeekService.getMedicalAdvice(prompt);

      if (mounted) {
        setState(() {
          _assessmentResults.addAll(response.split('\n'));
          _isGenerating = false;
        });
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _assessmentResults.add('生成风险评估结果时出错：$e');
          _isGenerating = false;
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: [
            // 顶部绿色背景区域
            Container(
              padding: const EdgeInsets.only(
                  left: 20, right: 20, bottom: 20, top: 10),
              color: const Color(0xFF1DC4A0),
              child: Row(
                children: [
                  IconButton(
                    icon: const Icon(Icons.arrow_back, color: Colors.white),
                    onPressed: () => Navigator.pop(context),
                    padding: EdgeInsets.zero,
                  ),
                  const SizedBox(width: 10),
                  const Text(
                    '风险评估结果',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),

            // 内容区域
            Expanded(
              child: SingleChildScrollView(
                padding: const EdgeInsets.all(20),
                child: _isGenerating
                    ? const Center(child: CircularProgressIndicator())
                    : Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: _assessmentResults
                            .map((line) => Padding(
                                  padding: const EdgeInsets.only(bottom: 10),
                                  child: Text(
                                    line,
                                    style: const TextStyle(fontSize: 16),
                                  ),
                                ))
                            .toList(),
                      ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
